var hlshost = "https://kaoshi.diuber.cn/PHPmvc/index.php?";
Page({

    data: {
        show: false,
        index: 0, // 默认选中的索引
        cityName: '',
        range: ['上海', '北京', '南京'], // 城市列表示例，实际应从服务器获取
        user: {
            name: '',
            phone: '',
            city: ''
        },
        safeTop: wx.getSystemInfoSync().safeArea.top
    },
    onLoad: function () {
        this.setData({
            cityName: this.data.range[this.data.index] // 默认选中第一个值
        });
    },
    onCityChange: function (e) {
        const selectedCityIndex = e.detail.value;
        const selectedCityName = this.data.range[selectedCityIndex];
        this.setData({
            cityName: selectedCityName,
            'user.city': selectedCityName
        });
    },
    onNameInput: function (e) {
        this.setData({
            'user.name': e.detail.value
        });
    },
    onPhoneInput: function (e) {
        this.setData({
            'user.phone': e.detail.value
        });
    },
    validateForm: function () {
        const {
            name,
            phone,
            city
        } = this.data.user;
        if (!name || name.trim().length === 0) {
            wx.showToast({
                title: '请输入姓名',
                icon: 'none'
            });
            return false;
        }

        if (!(/^1[3-9]\d{9}$/.test(phone))) {
            wx.showToast({
                title: '请输入有效的手机号码',
                icon: 'none'
            });
            return false;
        }
        /*
        if (!city || city.trim().length === 0) {
          wx.showToast({ title: '请选择城市名称', icon: 'none' });
          return false;
        }
        */
        return true;
    },

    submitForm: function () {
        if (!this.validateForm()) return;
        wx.showLoading({
            title: '正在提交...'
        });
        wx.request({
            url: hlshost + "c=joinus&m=index", // 替换为你的PHP接口地址
            method: "POST",

            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                name: this.data.user.name,
                phone: this.data.user.phone,
                city: this.data.user.city
            },
            success: function (res) {
                wx.hideLoading();
                if (res.statusCode === 200 && res.data.code == 0) {
                    that.setData({
                        show: true
                    });
                } else {
                    wx.showToast({
                        title: res.data.message || '你已经报过名了',
                        icon: 'none'
                    });
                }
            },
            fail: function (err) {
                wx.hideLoading();
                wx.showToast({
                    title: '网络请求失败，请检查网络连接',
                    icon: 'none'
                });
            }
        });
        // 绑定 this，防止内部函数丢失作用域
        const that = this;
    },
    hideAlert: function () {
        this.setData({
            show: false
        });
    },
    back() {
        wx.navigateBack()
    },
    onShareAppMessage: function () {
        return {
            title: "新⽼司机宝典，您的租⻋好帮⼿！",
            path: "pages/index/index",
            imageUrl: "/images/share.png"
        };
    }

})